//
//  WeiboController.m
//  ios_51job
//
//  Created by kai ren on 27/09/2011.
//  Copyright 2011 none. All rights reserved.
//

#import "WeiboController.h"
#import <QuartzCore/QuartzCore.h>

@implementation WeiboController

@synthesize OAuthInfo, content, delegate;
- (id)init{
	if (self = [super init]) {
		self.OAuthInfo = [self ReadOAuthInfo];
		self.content = @"";
		_data = [[NSMutableData alloc] initWithCapacity:0];
	}
	
	return self;
}

- (void)share{
}

- (void)getNick{
	
}

//Get catch path
- (NSString *)getCatchPath{
	return nil;
}

//Read OAuth info from document folder
- (NSMutableDictionary *)ReadOAuthInfo{
	NSString *path = [self getCatchPath];
	NSFileManager *appFileManager = [NSFileManager defaultManager];
	
	if ([appFileManager fileExistsAtPath:path] && [appFileManager isReadableFileAtPath:path]) {
		return [NSMutableDictionary dictionaryWithContentsOfFile:path];
	}else{
		return [NSMutableDictionary dictionaryWithCapacity:0];
	}	
}

- (void)dealloc{
	[super dealloc];
	
	[OAuthInfo release];
	[_data release];
}


#pragma mark -
#pragma mark NSURLConnection Delegate

/** Reset the received data **/
- (void)resetData {	
	[_data setLength:0];
}

/** Delegate: Call when Receive a new Response, Maybe called serveral times **/
- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response{
	[self resetData];
	
    /** Get response code **/
    NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
    int statusCode = [resp statusCode];
	
	NSLog(@"DEHttpConn::didReceiveResponse => HttpCode: %d.", statusCode);
	
    if (statusCode >= 400) {
		[conn cancel];
    } else if (statusCode == 304) {
		//TODO: if need?
		
		/** Destroy the connection. **/
        [conn cancel];
    }
}

/** Delegate: Append data to _data buffer **/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [_data appendData:data];
}

/** Delegate: Call when error happens **/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}

@end
